package com.lmf.cloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description TODO
 * <p>
 * Copyright @2022 Shanghai Mise Co. Ltd.
 * All right reserved.
 * <p>
 * @Author LiuMingFu
 * @Date 2024/7/22 21:33
 */
@Slf4j
//@RefreshScope   //在控制器类加入@RefreshScope注解使当前类下的配置支持Nacos的动态刷新功能，保持最新值
@RequestMapping("/nacos-sentinel")
@RestController
public class MyController {
    @GetMapping("/testA")
    public String testA() {
        return "------testA";
    }

    @GetMapping("/testB")
    public String testB() {
        return "------testB";
    }

    @SentinelResource(value = "ccc", blockHandler = "blockHandler", fallback = "fallback")
    @GetMapping("/testC")
    public String testC() {
        return "------testC";
    }

    @SentinelResource(value = "ddd", blockHandler = "blockHandler", fallback = "fallback")
    @GetMapping("/testD")
    public String testD() {
        int i = 10 / 0;
        return "------testD";
    }

    /**
     * 方法参数末尾必须携带 BlockException 类型参数，才能匹配该方法成功。即使是异常的顶级父类、父接口也不行；匹配不成功时根据fallback处理，或者默认规则返回；
     * 还有该方法返回值类型、参数数量与顺序、Sentinel控制台设置的流控规则命名也必须是@SentinelResource注解的value值，等等都要保持一致，否则也会匹配不成功。
     */
    public String blockHandler(BlockException t) {
        return "blockHandler异常处理信息，o(╥﹏╥)o";
    }

    public String fallback(Throwable t) {
        return "fallback异常处理信息，o(╥﹏╥)o";
    }

}
